// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MailAction.java

package com.iphaeton.lms.action.mail;

import com.iphaeton.common.helper.*;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.mail.IMailService;
import com.iphaeton.lms.business.organize.IUserService;
import com.iphaeton.lms.pojo.mail.Mail;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;

// Referenced classes of package com.iphaeton.lms.action.mail:
//            MailBizSupportAction

public class MailAction extends MailBizSupportAction {
	private static Logger logger;
	private DynaActionForm daf;
	private Pagination pageInfo;
	

	static {
		logger = Logger.getLogger(com.iphaeton.lms.action.mail.MailAction.class
				.getName());
	}

	

	public ActionForward newMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		daf = (DynaActionForm) form;
		String title = (String) daf.get("title");
		String comment = (String) daf.get("comment");
		FormFile file = (FormFile) daf.get("file");
		String id = request.getParameter("id");
		String rePage = "send_ok";
		Calendar cal = Calendar.getInstance();
		java.util.Date time = cal.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String sendTime = df.format(time);
		logger.info("\u53D1\u9001\u65F6\u95F4:" + sendTime);
		Mail m = new Mail();
		m.setTitle(title);
		if (comment != null && !"".equals(comment))
			m.setComment(comment);
		m.setState(new Integer(4));
		m.setSendTime(sendTime);
		try {
			UserSession userSession = (UserSession) request.getSession()
					.getAttribute("userSession");
			String sendId = userSession.getId();
			String sendUserId = userSession.getUserId();
			String sendName = userSession.getUserName();
			m.setSendId(sendId);
			m.setSendUid(sendUserId);
			m.setSendName(sendName);
			m = mail.createMail(m);
			if (id != null && !"".equals(id)) {
				String receiveId[] = id.split(",");
				for (int i = 0; i < receiveId.length; i++) {
					com.iphaeton.lms.pojo.organize.User u = user
							.findUserByUserId(receiveId[i]);
					mail.sendMail(m, u);
				}

				m.setState(new Integer(1));
				rePage = "send_ok";
			} else {
				m.setState(new Integer(2));
				rePage = "draft_ok";
			}
			if (file != null) {
				String mailUrl = request.getRealPath("/") + "upload"
						+ File.separator + "mail" + File.separator + m.getId()
						+ File.separator;
				logger.info("\u90AE\u4EF6\u9644\u4EF6:" + file.getFileName());
				if (!file.getFileName().equals("")
						&& file.getFileName() != null) {
					String tag = mailUrl + file.getFileName();
					int tagSize = file.getFileSize();
					m.setTag(tag);
					m.setSize(new Integer(tagSize));
				} else {
					m.setSize(new Integer(0));
				}
				if (!"".equals(file.getFileName()))
					upload.upload(file, mailUrl);
			}
			m = mail.createMail(m);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		} catch (NullPointerException ne) {
			return mapping.findForward("session_dead");
		}
		request.setAttribute("mail", m);
		return mapping.findForward(rePage);
	}

	public ActionForward findSendedMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		daf = (DynaActionForm) form;
		int currentPage = 0;
		int pageSize = 10;
		String strPage = (String) daf.get("page");
		String row = (String) daf.get("row");
		Integer state = (Integer) daf.get("state");
		String rePage = "mail_send";
		if (state != null && state.equals(new Integer(2)))
			rePage = "mail_draft";
		java.util.List list = null;
		try {
			UserSession userSession = (UserSession) request.getSession()
					.getAttribute("userSession");
			String userId = userSession.getId();
			if (strPage == null || strPage.trim().equals(""))
				currentPage = 1;
			else
				currentPage = Integer.parseInt(strPage);
			pageInfo = mail.findSendedMail(userId, row, state, pageSize);
			pageInfo.setPage(currentPage);
			pageInfo.setRow(row);
			request.getSession().setAttribute("pageInfo", pageInfo);
			list = mail.getSpecifiedMail(pageInfo);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		} catch (NullPointerException ne) {
			return mapping.findForward("session_dead");
		}
		request.setAttribute("list", list);
		return mapping.findForward(rePage);
	}

	public ActionForward findReceiveMail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		daf = (DynaActionForm) form;
		int currentPage = 0;
		int pageSize = 10;
		String strPage = (String) daf.get("page");
		String row = (String) daf.get("row");
		Integer state = (Integer) daf.get("state");
		if (state == null)
			state = new Integer(0);
		java.util.List list = null;
		try {
			UserSession userSession = (UserSession) request.getSession()
					.getAttribute("userSession");
			String userId = userSession.getId();
			if (strPage == null || strPage.trim().equals(""))
				currentPage = 1;
			else
				currentPage = Integer.parseInt(strPage);
			pageInfo = mail.findReceivedMail(userId, state, pageSize);
			pageInfo.setPage(currentPage);
			pageInfo.setRow(row);
			request.getSession().setAttribute("pageInfo", pageInfo);
			list = mail.findReceivedMail(userId, row, state, pageSize,
					currentPage);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		} catch (NullPointerException ne) {
			return mapping.findForward("session_dead");
		}
		request.setAttribute("state", state);
		request.setAttribute("list", list);
		return mapping.findForward("mail_receive");
	}

	public ActionForward findRemovedMail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		daf = (DynaActionForm) form;
		int currentPage = 0;
		int pageSize = 10;
		String strPage = (String) daf.get("page");
		String row = (String) daf.get("row");
		java.util.List list = null;
		try {
			UserSession userSession = (UserSession) request.getSession()
					.getAttribute("userSession");
			String userId = userSession.getId();
			if (strPage == null || strPage.trim().equals(""))
				currentPage = 1;
			else
				currentPage = Integer.parseInt(strPage);
			pageInfo = mail.findRemovedMail(userId, pageSize);
			pageInfo.setPage(currentPage);
			pageInfo.setRow(row);
			request.getSession().setAttribute("pageInfo", pageInfo);
			list = mail.findRemovedMail(userId, row, pageSize, currentPage);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		} catch (NullPointerException ne) {
			return mapping.findForward("session_dead");
		}
		request.setAttribute("list", list);
		return mapping.findForward("mail_remove");
	}

	public ActionForward removeSendedMail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String mailId = request.getParameter("mailId");
		try {
			mail.removeSendedMail(mailId);
		} catch (BusinessException e) {
			e.printStackTrace();
			return mapping.findForward("failure");
		}
		return mapping.findForward("mailList");
	}

	public ActionForward removeReceivedMail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String mailId = request.getParameter("mailId");
		try {
			UserSession userSession = (UserSession) request.getSession()
					.getAttribute("userSession");
			String userId = userSession.getId();
			mail.removeReceivedMail(mailId, userId);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		} catch (NullPointerException ne) {
			return mapping.findForward("session_dead");
		}
		return mapping.findForward("mailReceived");
	}

	public ActionForward removeDraftMail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String mailId = request.getParameter("mailId");
		try {
			mail.removeSendedMail(mailId);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		}
		return mapping.findForward("mailDraft");
	}

	public ActionForward getMailDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		java.util.List list = null;
		Mail m = null;
		String mailId = request.getParameter("mailId");
		try {
			UserSession userSession = (UserSession) request.getSession()
					.getAttribute("userSession");
			String userId = userSession.getId();
			m = mail.findMailById(mailId);
			list = mail.getMailDetail(mailId);
			mail.readedMark(mailId, userId);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		} catch (NullPointerException ne) {
			return mapping.findForward("session_dead");
		}
		request.setAttribute("mail", m);
		request.setAttribute("list", list);
		return mapping.findForward("mail_detail");
	}

	public ActionForward deleteMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String mailId = request.getParameter("mailId");
		try {
			UserSession userSession = (UserSession) request.getSession()
					.getAttribute("userSession");
			String userId = userSession.getId();
			if (mailId == null || mailId.equals(""))
				mail.clearMail(userId);
			else
				mail.deleteMail(userId, mailId);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		} catch (NullPointerException ne) {
			return mapping.findForward("session_dead");
		}
		return mapping.findForward("mailRemoved");
	}

	public ActionForward mailCount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		Integer count = null;
		Integer total = null;
		String user_id = ((UserSession) request.getSession().getAttribute(
				"userSession")).getId();
		try {
			total = mail.mailCount(user_id, "0");
			count = mail.mailCount(user_id, "1");
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		}
		request.setAttribute("mailCount", count);
		request.setAttribute("mailTotal", total);
		return mapping.findForward("mail_count");
	}

	public ActionForward reMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String id = request.getParameter("id");
		Mail m;
		try {
			m = mail.findMailById(id);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		}
		request.setAttribute("user", m.getSendUid());
		request.setAttribute("title", "\u56DE\u590D:" + m.getTitle());
		return mapping.findForward("mail_resend");
	}

	public ActionForward sendMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String id = request.getParameter("id");
		Mail m;
		try {
			m = mail.findMailById(id);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		}
		request.setAttribute("mail", m);
		return mapping.findForward("mail_draft_send");
	}

	public ActionForward sendDraft(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		daf = (DynaActionForm) form;
		UserSession userSession = (UserSession) request.getSession()
				.getAttribute("userSession");
		String sendId = userSession.getId();
		String sendUserId = userSession.getUserId();
		String sendName = userSession.getUserName();
		String title = (String) daf.get("title");
		String comment = (String) daf.get("comment");
		FormFile file = (FormFile) daf.get("file");
		String id = request.getParameter("id");
		String mail_id = request.getParameter("mail_id");
		Calendar cal = Calendar.getInstance();
		java.util.Date time = cal.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String sendTime = df.format(time);
		Mail m = null;
		if (file != null) {
			String mailUrl = request.getRealPath("/")
					+ "upload\\mail\\"
					+ sendUserId
					+ "\\"
					+ sendTime.replaceAll(" ", "").replaceAll("-", "")
							.replaceAll(":", "") + "\\";
			if (!file.getFileName().equals("") && file.getFileName() != null) {
				String tag = mailUrl + file.getFileName();
				int tagSize = file.getFileSize();
				m.setTag(tag);
				m.setSize(new Integer(tagSize));
			} else {
				m.setSize(new Integer(0));
			}
			if (!"".equals(file.getFileName()))
				try {
					upload.upload(file, mailUrl);
				} catch (BusinessException e1) {
					e1.printStackTrace();
					request.setAttribute("exception", e1);
					return mapping.findForward("failure");
				}
		}
		try {
			m = mail.findMailById(mail_id);
			m.setTitle(title);
			if (comment != null && !"".equals(comment))
				m.setComment(comment);
			m.setSendId(sendId);
			m.setSendUid(sendUserId);
			m.setSendName(sendName);
			m.setState(new Integer(4));
			m.setSendTime(sendTime);
			String receiveId[] = id.split(",");
			for (int i = 0; i < receiveId.length; i++) {
				com.iphaeton.lms.pojo.organize.User u = user
						.findUserByUserId(receiveId[i]);
				mail.sendMail(m, u);
			}

			m.setState(new Integer(1));
			m = mail.createMail(m);
		} catch (BusinessException e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		}
		request.setAttribute("mail", m);
		return mapping.findForward("send_ok");
	}

	public ActionForward download(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String id = request.getParameter("id");
		Mail m = null;
		String tag = "";
		try {
			m = mail.findMailById(id);
			tag = m.getTag();
			String name = tag.substring(tag.lastIndexOf(File.separatorChar), tag.length());
			request.setAttribute("url", tag);
			request.setAttribute("name", name);
			return mapping.findForward("download");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("exception", e);
			return mapping.findForward("failure");
		}
		
	}

	
}
